using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using ViewStateEliminator.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace ViewStateEliminator.Data
{
    /// <summary>
    /// Interface that must be implemented to create a Viewstate Storage Medium
    /// </summary>
    public interface IViewstateStorage
    {
        /// <summary>
        /// Adds a key-value pair to the storage medium.
        /// </summary>
        /// <param name="viewstateItem"></param>
        void Add(System.Collections.Generic.KeyValuePair<object, string> viewstateItem);
        /// <summary>
        /// Gets a key-value pair by a key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        System.Collections.Generic.KeyValuePair<object, string> Get(object key);
        /// <summary>
        /// Remove a key-value pair by key
        /// </summary>
        /// <param name="key"></param>
        void Remove(object key);
    }
    /// <summary>
    /// Factory Classes that returns a storage medium depending on values set in its configuration.
    /// </summary>
    public static class ViewstateStorageFactory
    {
        /// <summary>
        /// Gets a IViewstateStorage object from configuration file.
        /// </summary>
        /// <returns></returns>
        public static IViewstateStorage GetStorageMedium()
        {
            ViewStateConfiguration config = (ViewStateConfiguration)System.Configuration.ConfigurationManager.GetSection("ViewStateConfiguration");
            IViewstateStorage _retval = GetStorageMedium(config.EliminatorMode);
            if (_retval == null)
            {
                return new ViewStateSessionStorage();
            }
            else
                return _retval;                
        }
        /// <summary>
        /// Gets a IViewstateStorage object from a class 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IViewstateStorage GetStorageMedium(string type)
        {
            System.Type Itype = System.Type.GetType(type);
            return ((IViewstateStorage)Activator.CreateInstance(Itype));
        }
        /// <summary>
        /// Gets a IUserUniqueID object from configuration file.
        /// </summary>
        /// <returns></returns>
        public static IUserUniqueID GetUserUniqueID()
        {
            ViewStateConfiguration config = (ViewStateConfiguration)System.Configuration.ConfigurationManager.GetSection("ViewStateConfiguration");
            System.Type Itype = System.Type.GetType(config.UniqueIdSource);
            IUserUniqueID _retval = ((IUserUniqueID)Activator.CreateInstance(Itype));
            return _retval;
        }
    }
}
